package com.yohann.course_service.controller;

import com.yohann.course_service.entity.Chapter;
import com.yohann.course_service.entity.vo.ChapterInfo;
import com.yohann.course_service.service.ChapterService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 章节 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-10-18
 */
@Api(tags = "章节")
@RestController
@RequestMapping("/course_service/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    /**
     * 根据id查询章节信息
     *
     * @param id 章节id
     * @return 返回对应分类
     */
    @ApiOperation("根据id查询章节信息")
    @GetMapping("getChapterById/{id}")
    public Result getChapterById(@ApiParam("章节id") @PathVariable int id) {
        ChapterInfo chapter = chapterService.getChapterInfoById(id);

        return Result.ok().data("item", chapter);
    }

    /**
     * 查询课程的章节信息
     *
     * @param courseId 课程id
     * @return 课程章节信息
     */
    @ApiOperation("查询课程的章节信息")
    @GetMapping("getChapterInfo/{courseId}")
    public Result getChapterInfo(@ApiParam("课程id") @PathVariable int courseId) {
        List<ChapterInfo> chapterInfo = chapterService.getChapterInfo(courseId);

        return Result.ok().data("list", chapterInfo);
    }

    /**
     * 添加章节
     *
     * @param chapter 章节信息
     * @return 添加状态
     */
    @ApiOperation("添加章节")
    @PostMapping("addChapter")
    public Result addChapter(@ApiParam("章节信息") @RequestBody Chapter chapter) {
        boolean save = chapterService.save(chapter);

        return Result.state(save);
    }

    /**
     * 修改章节
     *
     * @param chapter 章节信息
     * @return 修改状态
     */
    @ApiOperation("修改章节")
    @PostMapping("updateChapter")
    public Result updateChapter(@ApiParam("章节信息") @RequestBody Chapter chapter) {
        boolean update = chapterService.updateById(chapter);

        return Result.state(update);
    }

    /**
     * 删除章节
     *
     * @param id 章节id
     * @return 删除状态
     */
    @ApiOperation("删除章节")
    @GetMapping("deleteChapter/{id}")
    public Result deleteChapter(@ApiParam("章节id") @PathVariable int id) {
        chapterService.deleteChapter(id);

        return Result.ok();
    }
}